//
//  GSCommonRegisterBindPhoneController.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/27.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonRegisterBindPhoneController.h"
//
#import "GSComomRegisterSMSVerifyController.h"

#import "GSCommonLoginPhoneTextField.h"

#import "GSCCommonRegisterBindVerifyController.h"

#import "NSString+RegexCategory.h"


@interface GSCommonRegisterBindPhoneController ()

@property (nonatomic, strong) GSCommonLoginPhoneTextField *phoneTF;

@end

@implementation GSCommonRegisterBindPhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.largeTitleLabel.text = GSLoginLocalized(@"gs_login_bind_account");
    [self setupSubviews];
    //
    //
    __weak typeof(self) wself = self;
    self.phoneTF.phoneInvalidBlock = ^(BOOL valid) {
        if (wself.nextStepButton.enabled!=valid) {
            wself.nextStepButton.enabled = valid;
        }
    };
    
    //唤起输入
    [self.phoneTF becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupSubviews {
    self.phoneTF = [GSCommonLoginPhoneTextField new];
    self.phoneTF.tintColor = self.phoneTF.textColor;
    self.phoneTF.adjustsFontSizeToFitWidth = true;
    self.phoneTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.phoneTF.autocorrectionType = UITextAutocorrectionTypeNo;
    self.phoneTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:GSLoginLocalized(@"gs_login_bind_account_placeholder") attributes:@{NSForegroundColorAttributeName:GSLOGIN_TEXTFIELD_PLACEHOLDER_COLOR,NSFontAttributeName:[UIFont systemFontOfSize:38 weight:UIFontWeightLight]}];
    self.phoneTF.keyboardType = UIKeyboardTypeEmailAddress;
    self.phoneTF.font = [UIFont systemFontOfSize:40 weight:UIFontWeightRegular];
    [self.view addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.largeTitleLabel.mas_bottom).offset(55.0);
        make.left.mas_equalTo(self.view).offset(18.0);
        make.right.mas_equalTo(self.view).offset(-18.0);
        make.height.mas_equalTo(56.0);
    }];
    //
    UIView *sepLine = [UIView new];
    sepLine.backgroundColor = GSLOGINRGBRGBHEX(0xF3F3F3);
    [self.view addSubview:sepLine];
    [sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneTF.mas_bottom).offset(3.0);
        make.left.mas_equalTo(self.view).offset(10.0);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(2.0);
    }];
    //
    [self.view addSubview:self.nextStepButton];
    self.nextStepButton.enabled = false;
    [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sepLine.mas_bottom).offset(40.0);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo([GSCommonLoginButton defaultSize]);
    }];
}

#pragma mark -
- (void)nextStepButtonDidClickedAction {
    [self.view endEditing:true];
    if ([self checkUserName]) {
        [self reqestCheckBinding:self.phoneTF.phoneNumber];
    }
}
//校验用户账户
- (BOOL)checkUserName {
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:self.phoneTF.phoneNumber];
    if (type==GSCCommonLoginAccountTypeUnknown) {
        NSString *errorMsg = GSLoginLocalized(@"gs_login_alert_account_error");
        [QMUITips showError:errorMsg inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorMsg]];
        return false;
    }
    return true;
}

//校验是否绑定
- (void)reqestCheckBinding:(NSString *)username {
    [self.view showLoginToast];
}
//请求验证码
- (void)requestCaptchaAction {
    NSString *username = self.phoneTF.phoneNumber;
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:username];
    if (type==GSCCommonLoginAccountTypePhone) {
        [self requestPhoneCaptcha:username];
    }else {
        [self requestEmailCaptchaAddress:username];
    }
}
//发送手机验证码
- (void)requestPhoneCaptcha:(NSString *)phone {
    
}
//发送邮箱验证码
- (void)requestEmailCaptchaAddress:(NSString *)email {
    
}

- (void)successToCheckAndSendCaptchaAction {
    [GSCommonLoginDataManager sharedData].bindingModel.username = self.phoneTF.phoneNumber;
    //
    GSCCommonRegisterBindVerifyController *bindVerifyVC = [GSCCommonRegisterBindVerifyController new];
    [self.navigationController pushViewController:bindVerifyVC animated:true];
}

@end
